package com.calsoft.Hops_Mobile_Tablet.component;
/*
 * User: Minh Diep
 * Date: 7/2/13
 * Time: 11:15 AM
 */

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.Log;
import com.calsoft.Hops_Mobile_Tablet.base.App;

import java.util.ArrayList;

public class CategoryChoiceDialogFragment extends DialogFragment {

    private boolean[] mSelectedItems;
    private Fragment fragment;

    public CategoryChoiceDialogFragment(Fragment fragment, boolean[] mSelectedItems) {
        this.mSelectedItems = new boolean[mSelectedItems.length];
        for (int i = 0; i < mSelectedItems.length; i++){
            this.mSelectedItems[i] = mSelectedItems[i];
        }
        this.fragment = fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        setRetainInstance(true);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Set the dialog title
        builder.setTitle("Show Categories")
                // Specify the list array, the items to be selected by default (null for none),
                // and the listener through which to receive callbacks when items are selected
                .setMultiChoiceItems(App.TOURS_CATEGORIES, mSelectedItems,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which,
                                                boolean isChecked) {
                            }
                        })
                        // Set the action buttons
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        mListener.onDialogPositiveClick(CategoryChoiceDialogFragment.this, mSelectedItems);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        mListener.onDialogNegativeClick(CategoryChoiceDialogFragment.this);
                    }
                });

        return builder.create();
    }

    public interface ChoiceListener{
        public void onDialogPositiveClick(DialogFragment dialog, boolean[] selectedItems);
        public void onDialogNegativeClick(DialogFragment dialog);
    }

    // Use this instance of the interface to deliver action events
    ChoiceListener mListener;

    // Override the Fragment.onAttach() method to instantiate the AlertDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (ChoiceListener) fragment;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement ChoiceListener");
        }
    }
}
